Foundation for many graphics applications:
ggplot(data=faithful)+
geom_point(mapping=aes(x=eruptions,
y=waiting))
ggplot()+
geom_point(data=faithful,mapping=aes(x=eruptions,
y=waiting))
ggplot(mpg) +
geom_bar(aes(x = class))
mpg_counted <- mpg %>%
count(class, name = 'count')
ggplot(mpg_counted) +
geom_bar(aes(x = class,
y = count),
stat = 'identity')
ggplot(mpg) +
geom_bar(aes(x = class,
y = after_stat(100 * count /
sum(count))))
Values calculated by the stat is available with the after_stat() function inside aes()
ggplot(mpg) +
geom_point(
aes(x = displ,
y = hwy,
colour = class))
aes() should happen. All mappings have an associated scale even if not specifiedfacet_null()) puts all the data in a single panelfacet_wrap() and facet_grid() allows you to specify different types of small multiples ggplot(mpg) +
geom_bar(aes(y = class)) +
facet_wrap(~year) +
labs(title = "Number of car models per class",
caption = "source: http://fueleconomy.gov",
x = NULL,
y = NULL) +
scale_x_continuous(expand = c(0, NA)) +
theme_minimal() +
theme(
text = element_text('Avenir Next Condensed'),
strip.text = element_text(face = 'bold', hjust = 0),
plot.caption = element_text(face = 'italic'),
panel.grid.major = element_line('white', size = 0.5),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.ontop = TRUE
)Take dataframe iris. Create scatter plot for Sepal.Length and Sepal.Width and label points with Species. Make sure the labels don’t overlap.
plot composition:
Patchwork will assign the same amount of space to each plot by default, but this can be controlled with the widths and heights argument in plot_layout(). This can take a numeric vector giving their relative sizes (e.g. c(2, 1) will make the first plot twice as big as the second). Modify the code below so that the middle plot takes up half of the total space:
p <- ggplot(mtcars) +
geom_point(aes(x = disp, y = mpg))
p + p + p
In the animation below (as in all the other animations) the changes happens at constant speed. How values change during an animation is called easing and can be controlled using the ease_aes() function. Read the documentation for ease_aes() and experiment with different easings in the animation, i.e. ease_aes(“bounce-in-out”)
mpg2 <- tidyr::pivot_longer(mpg, c(cty,hwy))
ggplot(mpg2) +
geom_point(aes(x = displ, y = value)) +
ggtitle("{if (closest_state == 'cty') 'Efficiency in city' else 'Efficiency on highway'}") +
transition_states(name)uros.godnov@gmail.com